#include "key_sample.h"
#include "usart.h"

static struct key_handle _key0;
static struct key_handle _key1;
static struct key_handle _wkup;

static void key0_event_callback(key_handle_t key)
{
    switch(key->event)
    {
    case Press_Down:
        UART_DEBUG("key0 press down");
    break;

    case Press_Up:
        UART_DEBUG("key0 press up");
    break;

    case Singe_Click:
        UART_DEBUG("key0 single click");
    break;

    case Double_Click:
        UART_DEBUG("key0 double click");
    break;

    case Short_Press_Repeat:
        UART_DEBUG("key0 short press repeat");
    break;

    case Long_Press_Start:
        UART_DEBUG("key0 long press start");
    break;

    case Long_Press_Hold:
        key_handle_detach(&_key0);
        UART_DEBUG("key0 long press hold");
    break;

    default:
    break;
    }
}

static void key1_event_callback(key_handle_t key)
{
    switch(key->event)
    {
    case Press_Down:
        UART_DEBUG("key1 press down");
    break;

    case Press_Up:
        UART_DEBUG("key1 press up");
    break;

    case Singe_Click:
        UART_DEBUG("key1 single click");
    break;

    case Double_Click:
        UART_DEBUG("key1 double click");
    break;

    case Short_Press_Repeat:
        UART_DEBUG("key1 short press repeat");
    break;

    case Long_Press_Start:
        UART_DEBUG("key1 long press start");
    break;

    case Long_Press_Hold:
        key_handle_detach(&_key1);
        UART_DEBUG("key1 long press hold");
    break;

    default:
    break;
    }
}

static void wkup_event_callback(key_handle_t key)
{
    switch(key->event)
    {
    case Press_Down:
        UART_DEBUG("wkup press down");
    break;

    case Press_Up:
        UART_DEBUG("wkup press up");
    break;

    case Singe_Click:
        UART_DEBUG("wkup single click");
    break;

    case Double_Click:
        UART_DEBUG("wkup double click");
    break;

    case Short_Press_Repeat:
        UART_DEBUG("wkup short press repeat");
    break;

    case Long_Press_Start:
        UART_DEBUG("wkup long press start");
    break;

    case Long_Press_Hold:
        key_handle_detach(&_wkup);
        UART_DEBUG("wkup long press hold");
    break;

    default:
    break;
    }
}

void key_test_sample(void)
{
    key_init(&_key1, key1_read_pin, GPIO_PIN_RESET);
    key_event_callback_register(&_key1, Press_Down,         key1_event_callback);
    key_event_callback_register(&_key1, Press_Up,           key1_event_callback);
    key_event_callback_register(&_key1, Singe_Click,        key1_event_callback);
    key_event_callback_register(&_key1, Double_Click,       key1_event_callback);
    key_event_callback_register(&_key1, Short_Press_Repeat, key1_event_callback);
    key_event_callback_register(&_key1, Long_Press_Start,   key1_event_callback);
    key_event_callback_register(&_key1, Long_Press_Hold,    key1_event_callback);
    key_handle_register(&_key1);

    key_init(&_key0, key0_read_pin, GPIO_PIN_RESET);
    key_event_callback_register(&_key0, Press_Down,         key0_event_callback);
    key_event_callback_register(&_key0, Press_Up,           key0_event_callback);
    key_event_callback_register(&_key0, Singe_Click,        key0_event_callback);
    key_event_callback_register(&_key0, Double_Click,       key0_event_callback);
    key_event_callback_register(&_key0, Short_Press_Repeat, key0_event_callback);
    key_event_callback_register(&_key0, Long_Press_Start,   key0_event_callback);
    key_event_callback_register(&_key0, Long_Press_Hold,    key0_event_callback);
    key_handle_register(&_key0);

    key_init(&_wkup, wkup_key_read_pin, GPIO_PIN_SET);
    key_event_callback_register(&_wkup, Press_Down,         wkup_event_callback);
    key_event_callback_register(&_wkup, Press_Up,           wkup_event_callback);
    key_event_callback_register(&_wkup, Singe_Click,        wkup_event_callback);
    key_event_callback_register(&_wkup, Double_Click,       wkup_event_callback);
    key_event_callback_register(&_wkup, Short_Press_Repeat, wkup_event_callback);
    key_event_callback_register(&_wkup, Long_Press_Start,   wkup_event_callback);
    key_event_callback_register(&_wkup, Long_Press_Hold,    wkup_event_callback);
    key_handle_register(&_wkup);
}
